package config_center

import (
	"fmt"
	"sync"
)

// AtomicBool bool类型的配置值
type AtomicBool struct {
	sync.RWMutex
	value    bool
	strValue string
}

func NewAtomicBool(val bool) *AtomicBool {
	return &AtomicBool{
		value:    val,
		strValue: fmt.Sprintf("%t", val),
	}
}

func (a *AtomicBool) Store(newStrValue string) bool {
	a.Lock()
	defer a.Unlock()

	if a.strValue == newStrValue {
		return false
	}

	a.strValue = newStrValue
	a.value = newStrValue == "true"

	return true
}

func (a *AtomicBool) String() string {
	a.RLock()
	defer a.RUnlock()

	return a.strValue
}

func (a *AtomicBool) Bool() bool {
	a.RLock()
	defer a.RUnlock()

	return a.value
}
